/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.model.entity.behaviours;

import ca.usherbrooke.model.entity.IEntityModel;

/**
 * @author Cody Stoutenburg
 * 
 */
public class JumpBehaviour extends EntityBehaviour {
	private static final long serialVersionUID = 1L;

	// Jump information
	private final int jumpReloadTimeMs;
	private int remainingJumpReloadTimeMs;
	private final int maxJumpDurationInMs;
	private int currentJumpDurationInMs;

	/**
	 * @param model
	 * @param jumpReloadTimeMs
	 * @param remainingJumpReloadTimeMs
	 * @param maxJumpDurationInMs
	 */
	public JumpBehaviour(IEntityModel model, int jumpReloadTimeMs,
			int maxJumpDurationInMs) {
		super(model);
		this.jumpReloadTimeMs = jumpReloadTimeMs;
		this.remainingJumpReloadTimeMs = 0;
		this.maxJumpDurationInMs = maxJumpDurationInMs;
		currentJumpDurationInMs = -1;
	}

	public void doLogic(int delta) {
		if (remainingJumpReloadTimeMs > 0) {
			remainingJumpReloadTimeMs -= delta;
			if (remainingJumpReloadTimeMs < 0) {
				remainingJumpReloadTimeMs = 0;
			}
		}

		// the jump logic
		if (currentJumpDurationInMs >= 0) {
			// set the jump speed
			currentJumpDurationInMs += delta;
			if (!isJumping()) {
				stopJump();
			}
		}
	}

	public void jump() {
		// verify that we are not jumping and that we have finish to reload jump
		if (!isJumping() && this.remainingJumpReloadTimeMs == 0) {
			currentJumpDurationInMs = 0;
		}
	}

	public void stopJump() {
		currentJumpDurationInMs = -1;
		remainingJumpReloadTimeMs = jumpReloadTimeMs;
	}

	public Boolean isJumping() {
		return 0 <= currentJumpDurationInMs
				&& currentJumpDurationInMs < maxJumpDurationInMs;
	}

	public Integer getCurrentJumpDuration() {
		return this.currentJumpDurationInMs;
	}

	public Integer getMaxJumpDuration() {
		return this.maxJumpDurationInMs;
	}

	public Integer getRemaningJumpReload() {
		return this.remainingJumpReloadTimeMs;
	}

	public Integer getJumpReloadTime() {
		return jumpReloadTimeMs;
	}
}
